package com.shuang.graph12;

import java.util.*;
 
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int pointNum = scanner.nextInt();
        father = new int[pointNum + 1];
        init();
        for (int i = 0; i < pointNum; i++) {
            int s = scanner.nextInt();
            int t = scanner.nextInt();
            if (isSame(s, t)){
                System.out.print(s + " " + t);
                return;
            }
            join(s, t);
        }
    }
 
    //并查集模板
    private static int[] father;
    //初始化
    private static void init(){
        for (int i = 1; i < father.length; i++) {
            // 让每个元素指向自己
            father[i] = i;
        }
    }
 
    //寻找根
    private static int find(int n){
        return n == father[n] ?  n : (father[n] = find(father[n]));
    }
 
    //在相同集合里
    private static boolean isSame(int n, int m){
        n = find(n);
        m = find(m);
 
        return n == m;
    }
 
    //加入到相同集合里
    private static void join(int n, int m){
        n = find(n);
        m = find(m);
 
        if (n == m){
            return;
        }
 
        father[n] = m;
    }
}